import { BasicColumn, FormSchema } from '/@/components/Table';
import { ProjectTypeList } from '/@/api/base/dictionary';
import { getAll as getAllClient } from '/@/api/business/client';
import { getList } from '/@/api/base/dictionary';
import { getCheckItemListByType } from '/@/api/project/planDetail';



/**
 * 表格列定义
*/
export const TableSchema: BasicColumn[] = [
  {
    title: '表单编号',
    dataIndex: 'formNo',
  },
  {
    title: '委托单位',
    dataIndex: 'clientName',
  },
  {
    title: '委托业务',
    dataIndex: 'projectType',
  },

];

/**
 * 搜索字段定义
*/
export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '关键字',
    component: 'Input',
    componentProps: {
      placeholder: '请输入名称/标识',
    },
    labelWidth: 60,
    colProps: { span: 6, lg: { span: 6, offset: 0 }, sm: { span: 10, offset: 0 }, xs: { span: 16, offset: 0 } },
  },
];

/**
 * 表单字段定义
*/
export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    show: false,
  },
  {
    field: 'formNo',
    label: '表单编号',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'clientId',
    label: '委托单位',
    required: true,
    component: 'ApiSelect',
    componentProps: {
      api: getAllClient,
      showSearch: true,
      optionFilterProp: 'label',
      resultField: 'data',
      labelField: 'name',
      valueField: 'id',
    },
  },
  {
    field: 'projectType',
    label: '委托业务',
    required: true,
    component: 'ApiSelect',
    componentProps: {
      api: ProjectTypeList,
      showSearch: true,
      optionFilterProp: 'label',
      resultField: 'value',
      labelField: 'value',
      valueField: 'value',
      onChange: (e) => {
        console.log('selected:', e);
      },
    },
  },
  {
    field: 'reasonNo',
    label: '编号',
    component: 'Input',
    defaultValue: 'AHJC/JL-13-01',
  },

  {
    field: 'detailList',
    label: '表单',
    component: 'Input',
    slot: 'detailList',
  },

];


/**
 * 采样信息：表单字段定义
*/
export const tableFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    show: false,
  },
  {
    field: 'reasonId',
    label: '委托依据ID',
    component: 'Input',
    show: false,
  },
  {
    field: 'checkType',
    label: '检测类别',
    required: true,
    component: 'ApiSelect',
    componentProps: ({ formActionType }) => ({
      disabled: true,
      api: getList,
      labelField: 'value',
      valueField: 'value',
      params: '检测类别',
      onChange: (type) => {
        console.log('1.检测类别变更:', type);
        const { updateSchema, } = formActionType;
        getCheckItemListByType(type).then(res => {
          //1. 获取检测项目列表
          updateSchema({
            field: 'checkItem',
            componentProps: {
              options: [],
            },
          });
          updateSchema({
            field: 'checkItem',
            componentProps: {
              options: res.map(item => ({ label: item.checkItem, value: item.checkItem })),
            }
          });
          console.log('获取检测项目成功', res);
        });
      },

    }),
  },

  {
    field: 'checkItem',
    label: '项目',
    component: 'Select',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'checkSubtype',
    label: '类别',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'checkStandard',
    label: '检测方法',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'collectWay',
    label: '采集或保存方法',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },

];